/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : join_room.js                                 
*  Description  : 조인룸 리스트                                       
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-04-03  김효순    Created.  
*  2012-04-04  김효순    검색바 추가          
*******************************************************************************/

jQuery(function($){
	// 나의 예약정보
	function clsMyReserve()
	{
		this._HTMLDom = null;
		this.listData = null;
		this._ERRMSG = {
			"_NOVALUE": "예약정보를 선택해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsMyReserve.prototype = {
		_init: function(){
			var $layer = $("div#container>section#main_content figure tr:first");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("option:first").clone(true)
			}
			
			$layer.find("div.reserve_in p").html("");
			$layer.find("select").empty();
		}
		, setList: function(thisData){
			this.listData = [
				{
					"키": null
					, "예약명": "나의 예약정보 선택"
					, "날짜": ""
					, "작성자": ""
					, "코스명": ""
					, "홀명": ""
				}
			].concat(thisData);
			this.display();
		}
		, display: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			
			$layer.find("select").empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.find("select").append($element.clone(true).attr("selected", ""));
			}
			$layer.find("select option").each(function(n){
				$(this).val(thisClass.listData[n]["키"]);
				$(this).html(thisClass.listData[n]["예약명"]);
			});
		}
		, setKey: function(key){
			var $layer = this._HTMLDom["$layer"];
			
			for(var i=0;i<this.listData.length;i++){
				if(this.listData[i]["키"] == key){
					break;
				}
			}
			
			if(i < this.listData.length){
				$layer.find("option:eq(" + i + ")").attr("selected", "selected");
			}
			
			this.$getSelect().trigger("change");
			
			//this.displayInfo();
		}
		, getKey: function(){
			return this._HTMLDom["$layer"].find("option:selected").val();
		}
		, displayInfo: function(){
			var $layer = this._HTMLDom["$layer"];
			var idx = $layer.find("option").index($layer.find("option:selected"));
			var $disp = $layer.find("div.reserve_in");
			
			$disp.find(".time:first").html(this.listData[idx]["날짜"]);
			$disp.find(".id:first").html(this.listData[idx]["작성자"]);
			$disp.find(".join_cor:first").html(this.listData[idx]["코스명"]);
			$disp.find(".join_cor:last").html(this.listData[idx]["홀명"]);
		}
		, $getSelect: function(){
			return this._HTMLDom["$layer"].find("select");
		}
		, isValid: function(){
			var keyVal = this.getKey();
			
			if(!keyVal){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
	}
	
	
	
	// 공개여부
	function clsOpen()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "공개여부를 선택해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsOpen.prototype = {
		_init: function(){
			this._HTMLDom = {
				"$radio": $(":radio[name=radio]")
			}
		}
		, setValue: function(val){
			if(val) this._HTMLDom["$radio"].eq(0).attr("checked", "checked");
			else this._HTMLDom["$radio"].eq(1).attr("checked", "checked");
		}
		, getValue: function(){
			return this._HTMLDom["$radio"].eq(0).is(":checked");
		}
		, isValid: function(){
			var keyVal = this.getValue();
			
			if(!keyVal){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
	}
	
	
	// 비밀번호
	function clsPasswd()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "비밀번호를 입력해 주십시요."
			, "_INVALID": "비밀번호를 4자리 숫자로 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsPasswd.prototype = {
		_init: function(){
			this._HTMLDom = {
				"$layer": $("#main_content").find(":password")
			}
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();;
		}
		, isValid: function(){
			var val = this.getValue();
			var reg = /^\d{4}$/ig;
			
			if(!val){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}else if(!reg.test(val)){
				this.errMsg = this._ERRMSG["_INVALID"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
	}
	
	
	// 인삿말
	function clsNote()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "인사말을 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsNote.prototype = {
		_init: function(){
			this._HTMLDom = {
				"$layer": $("textarea:first")
			}
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, isValid: function(){
			var val = this.getValue();
			
			if(!val){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
	}
	
	function clsComm()
	{
		this.reserveList = null;
		this.joinroomData = null;
		this.joinroomRequest = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.reserveList = [
				{
					"키": null
					, "예약명": null
					, "날짜": null
					, "작성자": null
					, "코스명": null
					, "홀명": null
				}
			];
			
			this.joinroomData = {
				"예약정보": null
				, "공개여부": null
				, "비밀번호": null
				, "인사말": null
			}
			
			this.joinroomRequest = {
				"키": null
			}
			
			this.joinroomSubmitRequest = {
				"예약정보": null
				, "공개여부": null
				, "비밀번호": null
				, "인사말": null
			}
			
			this.joinroomSubmitData = {
				"성공여부": null
				, "메시지": null
			}
		}
		, getReserveList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.reserveList = [
					{
						"키": "001"
						, "예약명": "나의 예약정보 001"
						, "날짜": "03.03(토)09:30"
						, "작성자": "Sky72"
						, "코스명": "오션코스"
						, "홀명": "11H"
					}
					, {
						"키": "002"
						, "예약명": "나의 예약정보 002"
						, "날짜": "03.01(토)09:30"
						, "작성자": "Sky72"
						, "코스명": "오션코스1"
						, "홀명": "12H"
					}
					, {
						"키": "003"
						, "예약명": "나의 예약정보 003"
						, "날짜": "03.02(토)09:30"
						, "작성자": "Sky72"
						, "코스명": "오션코스2"
						, "홀명": "13H"
					}
				];
				
				document.objControl.finishReserveList();
			}, 500);
		}
		, getReserveData: function(){
			return this.reserveList;
		}
		, getJoinroomInfo: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.joinroomData = {
					"예약정보": "001"
					, "공개여부": true
					, "비밀번호": "1234"
					, "인사말": "하이요"
				}
				
				document.objControl.finishJoinroomInfo();
			}, 1500);
		}
		, getJoinroomData: function(){
			return this.joinroomData;
		}
		, setJoinroomRequest: function(key, val){
			this.joinroomRequest[key] = val;
		}
		, joinroomSubmit: function(arg){
			var thisClass = this;
			
//			arg = {
//				"예약정보": null
//				, "공개여부": null
//				, "비밀번호": null
//				, "인사말": null
//			}
			
			//ajax
			setTimeout(function(){
				var ret = {
					"결과코드": "001"
					, "메시지": null
				}
				
				document.objControl.finishJoinroomSubmit(ret);
			}, 1000);
		}
	}
	
	function clsControl()
	{
		this.objComm = null;
		this.objMyReserve = null;
		this.objOpen = null;
		this.objPasswd = null;
		this.objNote = null;
		
		this.joinroomKey = document.objUtil.getParam("q_seq");
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objMyReserve = new clsMyReserve();
			this.objOpen = new clsOpen();
			this.objPasswd = new clsPasswd();
			this.objNote = new clsNote();
			
			this._bind();
			
			this.objComm.getReserveList();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 예약정보
			this.objMyReserve.$getSelect().change(function(){
				thisClass.objMyReserve.displayInfo();
			});
			
			// 인삿말
			this.objNote.$getLayer().keydown(function(event){
				if($(this).val().length >= 40 && event.keyCode > 47){
					event.preventDefault();
					return false;
				}
			});
			
			// 전송
			$("section#main_content p:last").click(function(){
				if(!thisClass.objMyReserve.isValid()){
					alert(thisClass.objMyReserve.getError());
					return;
				}else if(!thisClass.objOpen.isValid()){
					alert(thisClass.objOpen.getError());
					return;
				}else if(!thisClass.objPasswd.isValid()){
					alert(thisClass.objPasswd.getError());
					return;
				}else if(!thisClass.objNote.isValid()){
					alert(thisClass.objNote.getError());
					return;
				}
				
				document.objGlobal.spinStart("sendig...");
				thisClass.objComm.joinroomSubmit({
					"조인룸키": thisClass.joinroomKey
					, "예약정보": thisClass.objMyReserve.getKey()
					, "공개여부": thisClass.objOpen.getValue()
					, "비밀번호": thisClass.objPasswd.getValue()
					, "인사말": thisClass.objNote.getValue()
				});
			});
		}
		//예약정보완료
		, finishReserveList: function(){
			var thisData = this.objComm.getReserveData();
			
			this.objMyReserve.setList(thisData);
			this.objMyReserve.setKey(null);
			if(this.joinroomKey){
				document.objGlobal.spinStart("receiving...");
				this.objComm.setJoinroomRequest("키", this.joinroomKey);
				this.objComm.getJoinroomInfo();
			}
		}
		// 조인룸 정보완료
		, finishJoinroomInfo: function(){
			var thisData = this.objComm.getJoinroomData();
			
			document.objGlobal.spinEnd();
			
			this.objMyReserve.setKey(thisData["예약정보"]);
			this.objOpen.setValue(thisData["공개여부"]);
			this.objPasswd.setValue(thisData["비밀번호"]);
			this.objNote.setValue(thisData["인사말"]);
		}
		// 조인룸 만들기 완료
		, finishJoinroomSubmit: function(ret){
			document.objGlobal.spinEnd();
			
			if(ret["결과코드"]){
				document.location.href = "join_room.html";
			}else{
				alert(thisData["메시지"]);
			}
		}
	}
	
	document.objControl = new clsControl();
});